﻿using System;

namespace Cemit.NetFramework
{
    /// <summary>基于帧同步的倒计时</summary>
    public class FrameDelayTimer
    {
        private readonly float targetTime;
        private readonly Action action;

        private float time = 0;

        public FrameDelayTimer(float targetTime, Action action)
        {
            this.targetTime = targetTime;
            this.action = action;
        }

        public void Start()
        {
            FrameSyncManager.OnFrameUpdated += OnFrameUpdate;
        }

        private void OnFrameUpdate()
        {
            time += FrameSyncManager.DeltaTime;

            if (time >= targetTime)
            {
                action?.Invoke();
                FrameSyncManager.OnFrameUpdated -= OnFrameUpdate;
            }
        }
    }
}